<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

/**
 * Class User.
 * @property int id
 * @property string password
 * @property string name
 * @property string address
 * @property int identity
 * @property int role
 * @property int password_question_type
 * @property string password_question_answer
 * @property string wx_openid
 * @property string phone
 * @property string avatar
 * @property int admin_role_id
 * @property int sex
 * @property int is_company
 */
class User extends Authenticatable
{
    use HasFactory;
    use Notifiable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'date:Y-m-d H:i:s',
        'updated_at' => 'date:Y-m-d H:i:s',
    ];

    public function userProfile()
    {
        return $this->hasOne(UserProfile::class);
    }

    public function check()
    {
        return $this->hasOne(CheckUserProfile::class);
    }

    public function getAvatarAttribute($value)
    {
        if (empty($value)) {
            return 'https://image.hanlingsha.love/lwa/headpic/WechatIMG26383.jpeg';
        }
        if (substr($value, 0, 4) != 'http') {
            return config('app.url') . $value;
        }
        return $value;
    }

    public function house()
    {
        return $this->hasMany(House::class, 'houser_user_id');
    }
}
